package com.rtz.util;

import org.apache.log4j.Logger;

import java.math.BigDecimal;

public class Arith {
	private final static Logger logger = Logger.getLogger(Arith.class);
    public static void main(String[] args) {
    	int a=1;
    	int b=0;
        System.out.println(divToRate(a,b));
        
    }
    /**
     * 提供精确加法计算的add方法
     * 
     * @param value1
     *            被加数
     * @param value2
     *            加数
     * @return 两个参数的和
     */
    public static BigDecimal add(String value1, String value2) {
        BigDecimal b1 = new BigDecimal(String.valueOf(value1));
        BigDecimal b2 = new BigDecimal(String.valueOf(value2));
        BigDecimal b3 = b1.add(b2);
        BigDecimal result =b3.setScale(2, BigDecimal.ROUND_HALF_UP);
        return result;
    }
    /**
     * 提供精确加法计算的add方法
     * 
     * @param value1
     *            被加数
     * @param value2
     *            加数
     * @return 两个参数的和
     */
    public static BigDecimal add(BigDecimal value1, BigDecimal value2,int scale) {
        BigDecimal b3 = value1.add(value2);
        BigDecimal result =b3.setScale(scale, BigDecimal.ROUND_HALF_UP);
        return result;
    }

    /**
     * 提供精确减法运算的sub方法
     * 
     * @param value1
     *            被减数
     * @param value2
     *            减数
     * @return 两个参数的差
     */
    public static BigDecimal sub(String value1, String value2) {
        BigDecimal b1 = new BigDecimal(String.valueOf(value1));
        BigDecimal b2 = new BigDecimal(String.valueOf(value2));
        BigDecimal b3 = b1.subtract(b2);
        BigDecimal result =b3.setScale(2, BigDecimal.ROUND_HALF_UP);

        return result;
    }

    /**
     * 提供精确乘法运算的mul方法
     * 
     * @param value1
     *            被乘数
     * @param value2
     *            乘数
     * @return 两个参数的积
     */
    public static BigDecimal mul(String value1, String value2) {
        BigDecimal b1 = new BigDecimal(String.valueOf(value1));
        BigDecimal b2 = new BigDecimal(String.valueOf(value2));
        BigDecimal b3 = b1.multiply(b2);
        BigDecimal result =b3.setScale(2, BigDecimal.ROUND_HALF_UP);
        return result;
    }

    /**
     * 提供精确的除法运算方法div(默认保留两位小数,四舍五入)
     * 
     * @param value1
     * @param value2
     * @return
     */
    public static BigDecimal div(String value1, String value2) {
        BigDecimal b1 = new BigDecimal(String.valueOf(value1));
        BigDecimal b2 = new BigDecimal(String.valueOf(value2));
        BigDecimal b3 = b1.divide(b2,2,BigDecimal.ROUND_HALF_UP);
        BigDecimal result =b3.setScale(2, BigDecimal.ROUND_HALF_UP);
        return result;
    }
    
    /**
     * 提供精确的除法运算方法div
     * @param value1 
     * @param value2 
     * @param scale 精度
     * @return
     */
    public static BigDecimal div(String value1, String value2,int scale) {
        BigDecimal b1 = new BigDecimal(String.valueOf(value1));
        BigDecimal b2 = new BigDecimal(String.valueOf(value2));
        BigDecimal b3 = new BigDecimal("0.00");
        try {
             b3 = b1.divide(b2,scale,BigDecimal.ROUND_HALF_UP);
        } catch (Exception e) {
        }
        BigDecimal result =b3.setScale(scale, BigDecimal.ROUND_HALF_UP);
        return result;
    }
    /**
     * 提供精确的除法运算方法div (总是在非  0 舍弃小数(即截断)之前增加数字)
     * @param value1
     * @param value2
     * @param scale
     * @return
     */
    public static BigDecimal divRoundUp(String value1, String value2,int scale) {
        BigDecimal b1 = new BigDecimal(String.valueOf(value1));
        BigDecimal b2 = new BigDecimal(String.valueOf(value2));
        BigDecimal b3 = new BigDecimal("0.00");
        try {
             b3 = b1.divide(b2,scale,BigDecimal.ROUND_UP);
        } catch (Exception e) {
        }
        BigDecimal result =b3.setScale(scale, BigDecimal.ROUND_UP);
        return result;
    }
    

    public static BigDecimal add2(BigDecimal value1, String value2) {
        BigDecimal b2 = new BigDecimal(String.valueOf(value2));
        BigDecimal b3 = value1.add(b2);
        BigDecimal result =b3.setScale(2, BigDecimal.ROUND_HALF_UP);
        return result;
    }

    /**
     * 提供精确乘法运算的mul方法 四舍五入取整 返回string
     * 
     * @param value1
     *            被乘数
     * @param value2
     *            乘数
     * @return 两个参数的积
     */
    public static String mul2(String value1, String value2) {
        BigDecimal b1 = new BigDecimal(String.valueOf(value1));
        BigDecimal b2 = new BigDecimal(String.valueOf(value2));
        BigDecimal b3 = b1.multiply(b2);
        String b4 = b3.setScale(0, BigDecimal.ROUND_HALF_UP).toString();
        return b4;
    }

    /**
     * 提供精确加法计算的div方法 int
     * 
     */
    public static BigDecimal divToRate(int value1, int value2) {
        BigDecimal b1 = new BigDecimal(String.valueOf(value1));
        BigDecimal b2 = new BigDecimal(String.valueOf(value2));
        BigDecimal b3 = new BigDecimal("0.0000");
        try {
             b3 = b1.divide(b2,6,BigDecimal.ROUND_HALF_UP);
        } catch (Exception e) {
        }
        b3.setScale(6, BigDecimal.ROUND_HALF_UP);
        b3=b3.multiply(new BigDecimal("100"));
        b3=b3.setScale(4, BigDecimal.ROUND_HALF_UP);
        return b3;
    }
    public static BigDecimal divToRate(long value1, long value2) {
        BigDecimal b1 = new BigDecimal(String.valueOf(value1));
        BigDecimal b2 = new BigDecimal(String.valueOf(value2));
        BigDecimal b3 = new BigDecimal("0.0000");
        try {
             b3 = b1.divide(b2,6,BigDecimal.ROUND_HALF_UP);
        } catch (Exception e) {
        }
        b3.setScale(6, BigDecimal.ROUND_HALF_UP);
        b3=b3.multiply(new BigDecimal("100"));
        b3=b3.setScale(4, BigDecimal.ROUND_HALF_UP);
        return b3;
    }
}